import Paging from '../../../wxjs/Paging.js'
import getLocation from '../../../wxjs/getLocation.js'
import navToNext from '../../../wxjs/navToNext.js'
const app = getApp();
var pag = new Paging();


var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  bodyMsg: "请稍等...",
};

var currentPage = {
  data,
  reqUrl: app.serAddrs.getLectureList,
  form: {}, //详情请求参数
  onLoad,
  selectDay,
  completeLocation, //定位获取完成（成功，失败）
  getDetail, //获取详情
  openLocation,
  onShareAppMessage: function () {
    const that = this;
    const uuid = that.data.uuid;
    return {
      title: '',
      path: app.xhx_route.storeList + "?uuid=" + uuid
    }
  }
};

var page = Object.assign({}, pag.func(), getLocation, navToNext, currentPage);

Page(page);

function onLoad(options) {
  const that = this;
  const uuid = options && options.uuid || '';

  that.setData({
    uuid
  })
  that.form.gymUuid = uuid;

  that.listParams.latitude = '';
  that.listParams.longitude = '';
  that.listParams.areas = [];
  that.listParams.gymTypeIds = [];
  that.listParams.intervalTimes = [];
  that.listParams.tGoodsLabelNameUuids = [];
  that.listParams.tGymUuids = [uuid];
  that.listParams.isShadowOver = 0;
  that.getLocation();
}

function selectDay(event) {
  const that = this;
  const time = event.detail.time;
  that.listParams.dateTime = time || '';
  // 初始化
  that.pageListInit();
}

function completeLocation(res) {
  const that = this;
  let latitude = res.latitude || '';
  let longitude = res.longitude || '';

  that.form.latitude = latitude;
  that.form.longitude = longitude;
  that.getDetail();
  that.setData({
    latitude,
    longitude
  })
}

function getDetail() {
  const that = this;
  app.middleware.requestAjax({
    serAddr: app.serAddrs.getGymDetail,
    data: that.form,
    cancelToast: true,
    success: function(res) {
      let xbody = res.data;

      try {
        xbody.images = JSON.parse(xbody.images);
      } catch (e) {
        xbody.images = [];
      }
      try {
        xbody.videos = JSON.parse(xbody.videos);
      } catch (e) {
        xbody.videos = [];
      }

      if (xbody.equipPoints){
        xbody.equipPoints = xbody.equipPoints.split(',');
      }else{
        xbody.equipPoints = [];
      }

      if (xbody.gymType) {
        xbody.gymType = xbody.gymType.split(',');
      } else {
        xbody.gymType = [];
      }

      if (xbody.gymLables) {
        xbody.gymLables = xbody.gymLables.split(',');
      } else {
        xbody.gymLables = [];
      }

      that.setData({
        bodyMsg: "",
        xbody,
      })
    },
    fail: function(res) {
      that.setData({
        bodyMsg: res && res.msg || "请求失败"
      })
    }
  })
}

function openLocation(event) {
  const that = this;
  const xbody = that.data.xbody;
  const latitude = xbody.latitude;
  const longitude = xbody.longitude;
  app.middleware.openLocation({
    latitude,
    longitude,
  });
}